package com.yb.xczx.cloud.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yb.xczx.cloud.auth.bean.XczxSysUser;
import com.yb.xczx.cloud.auth.entity.SysPermission;
import com.yb.xczx.cloud.auth.entity.SysRolePermission;
import com.yb.xczx.cloud.auth.entity.SysUser;
import com.yb.xczx.cloud.auth.entity.SysUserRole;
import com.yb.xczx.cloud.auth.mapper.SysPermissionMapper;
import com.yb.xczx.cloud.auth.mapper.SysRolePermissionMapper;
import com.yb.xczx.cloud.auth.mapper.SysUserMapper;
import com.yb.xczx.cloud.auth.mapper.SysUserRoleMapper;
import com.yb.xczx.cloud.auth.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 姚兵
 * @since 2023-05-17
 */
//@Service
@Slf4j
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    SysUserRoleMapper sysUserRoleMapper;


    @Autowired
    SysRolePermissionMapper sysRolePermissionMapper;


    @Autowired
    SysPermissionMapper sysPermissionMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        log.info("load user username:{}",username);

        if(!StringUtils.hasText(username)){
            throw new UsernameNotFoundException("用户名不能为空");
        }


        SysUser sysUser = this.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, username));

        if(Objects.isNull(sysUser)){
            throw new UsernameNotFoundException("用户不存在");
        }
        Integer userId = sysUser.getUserId();
        //根据userId查询permission

        List<SysUserRole> sysUserRoles = sysUserRoleMapper.selectList(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId, userId));

        List<Integer> roleIds = sysUserRoles.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());

        List<SysRolePermission> rolePermissions = sysRolePermissionMapper.selectList(new LambdaQueryWrapper<SysRolePermission>().in(SysRolePermission::getRoleId, roleIds));

        List<Integer> permissionids = rolePermissions.stream().map(SysRolePermission::getPermissionId).collect(Collectors.toList());

        List<SysPermission> sysPermissions = sysPermissionMapper.selectList(new LambdaQueryWrapper<SysPermission>().in(SysPermission::getPermissionId, permissionids));
        List<String> perms = sysPermissions.stream().map(SysPermission::getPermissionName).collect(Collectors.toList());
        XczxSysUser xczxSysUser = new XczxSysUser(sysUser.getUsername(), sysUser.getPassword(), AuthorityUtils.createAuthorityList(perms.toArray(new String[0])));
        xczxSysUser.setPermissions(sysPermissions);
        xczxSysUser.setSysUser(sysUser);

        return xczxSysUser;
    }
}
